import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

class Solution {
    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> list = new ArrayList<>();
        HashMap<Character, Integer> hashMap1 = new HashMap<>();
        HashMap<Character, Integer> hashMap2 = new HashMap<>();

        for (int i = 0; i < p.length(); i++) {
            char c = p.charAt(i);
            hashMap1.put(c, hashMap1.getOrDefault(c, 0) + 1);
        }
        for (int left = 0, right = 0; right < s.length(); right++) {
            //进窗口
            hashMap2.put(s.charAt(right), hashMap2.getOrDefault(s.charAt(right), 0) + 1);
            if (right - left + 1 > p.length()) {
                char c = s.charAt(left++);
                if (hashMap2.get(c) == 1) {
                    hashMap2.remove(c);
                } else {
                    hashMap2.put(c, hashMap2.get(c) - 1);
                }
            }
            if (hashMap1.equals(hashMap2)) {
                list.add(left);
            }
        }

        return list;
    }

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.findAnagrams("aa", "bb"));
    }
}